# 使用 CentOS 8 作为基础镜像
FROM centos:8
# 设置维护者信息（可选）
LABEL maintainer="yourname@example.com"
# 设置 DNS 配置
RUN echo "nameserver 223.5.5.5" > /etc/resolv.conf && \
    echo "nameserver 8.8.8.8" >> /etc/resolv.conf
# 修复 CentOS 8 软件源
RUN sed -i -e "s|^mirrorlist=|#mirrorlist=|" \
           -e "s|^#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|" \
        /etc/yum.repos.d/CentOS-*.repo
# 配置 EPEL 源（使用阿里云镜像）- 修改后的可靠方法
RUN echo "nameserver 223.5.5.5" > /etc/resolv.conf && \
    echo "nameserver 8.8.8.8" >> /etc/resolv.conf && \
    curl --retry 3 -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-8.repo && \
    sed -i 's|^metalink|#metalink|g; s|^#baseurl=.*/epel|baseurl=https://mirrors.aliyun.com/epel|g' /etc/yum.repos.d/epel.repo
# 安装系统工具和依赖
RUN dnf clean all && \
    dnf makecache && \
    dnf install -y \
        wget \
        bzip2 \
        gcc \
        openssl-devel \
        libffi-devel \
        libtirpc \
        make \
        zlib-devel \
        bzip2-devel \
        readline-devel \
        sqlite \
        sqlite-devel \
        tk-devel \
        xz-devel \
        iotop \
        vim \
        --nogpgcheck
# 添加并解压宿主机环境配置包（需提前准备好 /tmp/prod-sync.tar.gz）
COPY prod-sync.tar.gz /tmp/
RUN mkdir -p /sync && \
      tar -xzf /tmp/prod-sync.tar.gz -C /sync && \
      rm -f /tmp/prod-sync.tar.gz
# 下载并编译安装 Python 3.10
RUN PYTHON_VERSION="3.10.13" && \
    PYTHON_URL="https://registry.npmmirror.com/-/binary/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.xz" && \
    mkdir -p /opt && \
    cd /opt && \
    wget --tries=3 --timeout=30 "${PYTHON_URL}" -O /tmp/python.tar.xz && \
    tar -xJf /tmp/python.tar.xz && \
    cd /opt/Python-${PYTHON_VERSION} && \
    ./configure \
        --prefix=/opt/python3.10 \
        --enable-optimizations \
        --enable-shared \
        --with-system-ffi \
        --with-ssl-default-suites=openssl \
        LDFLAGS="-L/usr/lib64" \
        CPPFLAGS="-I/usr/include/tirpc" \
        --with-tirpc && \
    make -j$(nproc) && \
    make install && \
    cd /opt && \
    rm -rf /opt/Python-${PYTHON_VERSION} /tmp/python.tar.xz
# 配置 Python 环境
RUN ln -sf /opt/python3.10/bin/python3.10 /usr/bin/python3 && \
    ln -sf /opt/python3.10/bin/python3.10 /usr/bin/python && \
    ln -sf /opt/python3.10/bin/pip3.10 /usr/bin/pip3 && \
    ln -sf /opt/python3.10/bin/pip3.10 /usr/bin/pip && \
    echo "/opt/python3.10/lib" > /etc/ld.so.conf.d/python3.10.conf && \
    ldconfig
# 应用系统优化配置
COPY sysctl.conf /etc/sysctl.conf
COPY limits.conf /etc/security/limits.conf
# 设置容器启动时的默认命令
CMD ["/bin/bash", "-c", "for conf in /sync/sysctl.conf /sync/sysctl.d/*; do [ -f \"\$conf\" ] && sysctl -p \"\$conf\" >/dev/null 2>&1; done && [ -f /sync/limits.conf ] && cat /sync/limits.conf > /etc/security/limits.conf && exec /bin/bash"]
